<?php

namespace App\Http\Controllers\Index;

use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyJob;
use Style, DB, Config;
use Illuminate\Http\Request;
use App\Repositories\Eloquent\WeiPin;

class MicroController extends CommonController{
    /**
     * 微点赞
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function microLike(Request $request){
        if(!empty($request->com_id)){
            $result = CompanyBasic::where('id', $request->com_id)->increment('micro_like', 1);
            if($result){
                $request->session()->put('like', '1');
                return response()->json(['status' => 0, 'msg' => '点赞成功']);
            }else{
                return response()->json(['status' => 1, 'msg' => '这个公司不存在！']);
            }
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function microRecruitment($com_id,Request $request){
        $com_id = idDecryption($com_id);
        $companyBasic = CompanyBasic::select('id', 'company_name', 'comkind', 'employee_num', 'temptation', 'profile',
            'mobile', 'phone', 'region', 'address', 'logo', 'micro_like')->where('id', '=', $com_id)->first();//公司信息

        if(!empty($companyBasic)){
            $companyBasic->comkind = !empty($companyBasic->comkind) ? CollOption::getOptionName($companyBasic->comkind, 'comkind') : '';
            $companyBasic->employee_num = !empty($companyBasic->employee_num) ? CollOption::getOptionName($companyBasic->employee_num, 'employee_num') : '';
            $companyBasic->phone = !empty($companyBasic->mobile) ? $companyBasic->mobile : $companyBasic->phone;
            $companyBasic->region = !empty($companyBasic->region) ? CollArea::getAreaName($companyBasic->region) : '';
            $companyBasic->logo = !empty($companyBasic->logo) ? 'upload/companylogo/' . $companyBasic->logo : 'default/images/company/wzp_logo_v2.png';
            //公司诱惑
            $temptation = array_values(array_filter(explode(',', $companyBasic->temptation)));
            if(count($temptation) > 0){
                $temptationArr = [];
                $i = 0.5;
                foreach($temptation as $k => $v){
                    if($k % 2 == 0 && $k != 0){
                        $i += 0.1;
                    }
                    $temptationArr[$k]['delay'] = $i;
                    $temptationArr[$k]['name'] = $v;
                }
                $companyBasic->temptation = $temptationArr;
            }
            $companyJob = CompanyJob::select('id', 'job_title', 'education', 'work_year', 'salary_above', 'salary_below',
                'location1', 'location2', 'location3', 'location4', 'location5', 'contact_info', 'description')
                ->where('com_id', $companyBasic->id)->where('job_status', '1')->get();
            if(count($companyJob) > 0){
                foreach($companyJob as $v){
                    $v->salary_above = !empty($v->salary_above) ? CollOption::getOptionName($v->salary_above, 'expectedsalary') : '';
                    $v->salary_below = !empty($v->salary_below) ? CollOption::getOptionName($v->salary_below, 'expectedsalary') : '';
                    $v->money = ($v->salary_above) ? $v->salary_above . '~' . $v->salary_below . '元/月' : '面议';
                    $location1 = !empty($v->location1) ? CollArea::getAreaName($v->location1) : '';
                    $location2 = !empty($v->location2) ? CollArea::getAreaName($v->location2) : '';
                    $location3 = !empty($v->location3) ? CollArea::getAreaName($v->location3) : '';
                    $location4 = !empty($v->location4) ? CollArea::getAreaName($v->location4) : '';
                    $location5 = !empty($v->location5) ? CollArea::getAreaName($v->location5) : '';
                    $v->location = trim($location1 . ',' . $location2 . ',' . $location3 . ',' . $location4 . ',' . $location5, ',');
                    $v->work_year = !empty($v->work_year) ? CollOption::getOptionName($v->work_year, 'work_year') : '经验不限';
                    $v->education = !empty($v->education) ? CollOption::getOptionName($v->education, 'education') : '学历不限';
                }
            }
        }
        $jssdk = new WeiPin();//微信分享接口
        $signPackage = $jssdk->GetSignPackage();
        return Style::view('index.microRecruitment', compact('companyBasic', 'companyJob', 'signPackage'));
    }

}
